// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso;

import java.io.File;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import zombie.Lua.LuaEventManager;
import zombie.characters.IsoPlayer;
import zombie.core.Core;
import zombie.core.Rand;
import zombie.core.properties.PropertyContainer;
import zombie.debug.DebugLog;
import zombie.iso.SpriteDetails.IsoFlagType;
import zombie.iso.SpriteDetails.IsoObjectType;
import zombie.iso.areas.IsoRoom;
import zombie.iso.objects.IsoBarbecue;
import zombie.iso.objects.IsoClothingDryer;
import zombie.iso.objects.IsoClothingWasher;
import zombie.iso.objects.IsoCombinationWasherDryer;
import zombie.iso.objects.IsoCurtain;
import zombie.iso.objects.IsoDoor;
import zombie.iso.objects.IsoFireplace;
import zombie.iso.objects.IsoJukebox;
import zombie.iso.objects.IsoLightSwitch;
import zombie.iso.objects.IsoMannequin;
import zombie.iso.objects.IsoRadio;
import zombie.iso.objects.IsoStove;
import zombie.iso.objects.IsoTelevision;
import zombie.iso.objects.IsoTree;
import zombie.iso.objects.IsoWheelieBin;
import zombie.iso.objects.IsoWindow;
import zombie.iso.sprite.IsoDirectionFrame;
import zombie.iso.sprite.IsoSprite;
import zombie.iso.sprite.IsoSpriteInstance;
import zombie.iso.sprite.IsoSpriteManager;
import zombie.network.GameClient;
import zombie.network.GameServer;

public final class CellLoader {
    public static final ArrayDeque<IsoObject> isoObjectCache = new ArrayDeque();
    public static final ArrayDeque<IsoTree> isoTreeCache = new ArrayDeque();
    static int wanderX = 0;
    static int wanderY = 0;
    static IsoRoom wanderRoom = null;
    static final HashSet<String> missingTiles = new HashSet();
    public static final HashMap<IsoSprite, IsoSprite> smashedWindowSpriteMap = new HashMap();

    public static void DoTileObjectCreation(IsoSprite spr, IsoObjectType type, IsoGridSquare sq, IsoCell cell, int x, int y, int height, String name) throws NumberFormatException {
        IsoObject isoObject0 = null;
        if (sq != null) {
            boolean boolean0 = false;
            if (smashedWindowSpriteMap.containsKey(spr)) {
                spr = (IsoSprite)smashedWindowSpriteMap.get(spr);
                type = spr.getType();
                boolean0 = true;
            }

            PropertyContainer propertyContainer = spr.getProperties();
            if (spr.solidfloor && propertyContainer.Is(IsoFlagType.diamondFloor) && !propertyContainer.Is(IsoFlagType.transparentFloor)) {
                IsoObject isoObject1 = sq.getFloor();
                if (isoObject1 != null && isoObject1.getProperties().Is(IsoFlagType.diamondFloor)) {
                    isoObject1.clearAttachedAnimSprite();
                    isoObject1.setSprite(spr);
                    return;
                }
            }

            if (type == IsoObjectType.doorW || type == IsoObjectType.doorN) {
                IsoDoor isoDoor = new IsoDoor(cell, sq, spr, type == IsoObjectType.doorN);
                isoObject0 = isoDoor;
                AddSpecialObject(sq, isoDoor);
                if (spr.getProperties().Is("DoubleDoor")) {
                    isoDoor.Locked = false;
                    isoDoor.lockedByKey = false;
                }

                if (spr.getProperties().Is("GarageDoor")) {
                    isoDoor.Locked = !isoDoor.IsOpen();
                    isoDoor.lockedByKey = false;
                }

                GameClient.instance.objectSyncReq.putRequest(sq, isoDoor);
            } else if (type == IsoObjectType.lightswitch) {
                isoObject0 = new IsoLightSwitch(cell, sq, spr, sq.getRoomID());
                AddObject(sq, isoObject0);
                GameClient.instance.objectSyncReq.putRequest(sq, isoObject0);
                if (isoObject0.sprite.getProperties().Is("lightR")) {
                    float float0 = Float.parseFloat(isoObject0.sprite.getProperties().Val("lightR")) / 255.0F;
                    float float1 = Float.parseFloat(isoObject0.sprite.getProperties().Val("lightG")) / 255.0F;
                    float float2 = Float.parseFloat(isoObject0.sprite.getProperties().Val("lightB")) / 255.0F;
                    int int0 = 10;
                    if (isoObject0.sprite.getProperties().Is("LightRadius") && Integer.parseInt(isoObject0.sprite.getProperties().Val("LightRadius")) > 0) {
                        int0 = Integer.parseInt(isoObject0.sprite.getProperties().Val("LightRadius"));
                    }

                    IsoLightSource isoLightSource = new IsoLightSource(
                        isoObject0.square.getX(), isoObject0.square.getY(), isoObject0.square.getZ(), float0, float1, float2, int0
                    );
                    isoLightSource.bActive = true;
                    isoLightSource.bHydroPowered = true;
                    isoLightSource.switches.add((IsoLightSwitch)isoObject0);
                    ((IsoLightSwitch)isoObject0).lights.add(isoLightSource);
                } else {
                    ((IsoLightSwitch)isoObject0).lightRoom = true;
                }
            } else if (type != IsoObjectType.curtainN && type != IsoObjectType.curtainS && type != IsoObjectType.curtainE && type != IsoObjectType.curtainW) {
                if (spr.getProperties().Is(IsoFlagType.windowW) || spr.getProperties().Is(IsoFlagType.windowN)) {
                    isoObject0 = new IsoWindow(cell, sq, spr, spr.getProperties().Is(IsoFlagType.windowN));
                    if (boolean0) {
                        ((IsoWindow)isoObject0).setSmashed(true);
                    }

                    AddSpecialObject(sq, isoObject0);
                    GameClient.instance.objectSyncReq.putRequest(sq, isoObject0);
                } else if (spr.getProperties().Is(IsoFlagType.container) && spr.getProperties().Val("container").equals("barbecue")) {
                    isoObject0 = new IsoBarbecue(cell, sq, spr);
                    AddObject(sq, isoObject0);
                } else if (spr.getProperties().Is(IsoFlagType.container) && spr.getProperties().Val("container").equals("fireplace")) {
                    isoObject0 = new IsoFireplace(cell, sq, spr);
                    AddObject(sq, isoObject0);
                } else if ("IsoCombinationWasherDryer".equals(spr.getProperties().Val("IsoType"))) {
                    isoObject0 = new IsoCombinationWasherDryer(cell, sq, spr);
                    AddObject(sq, isoObject0);
                } else if (spr.getProperties().Is(IsoFlagType.container) && spr.getProperties().Val("container").equals("clothingdryer")) {
                    isoObject0 = new IsoClothingDryer(cell, sq, spr);
                    AddObject(sq, isoObject0);
                } else if (spr.getProperties().Is(IsoFlagType.container) && spr.getProperties().Val("container").equals("clothingwasher")) {
                    isoObject0 = new IsoClothingWasher(cell, sq, spr);
                    AddObject(sq, isoObject0);
                } else if (spr.getProperties().Is(IsoFlagType.container) && spr.getProperties().Val("container").equals("woodstove")) {
                    isoObject0 = new IsoFireplace(cell, sq, spr);
                    AddObject(sq, isoObject0);
                } else if (!spr.getProperties().Is(IsoFlagType.container)
                    || !spr.getProperties().Val("container").equals("stove") && !spr.getProperties().Val("container").equals("microwave")) {
                    if (type == IsoObjectType.jukebox) {
                        isoObject0 = new IsoJukebox(cell, sq, spr);
                        isoObject0.OutlineOnMouseover = true;
                        AddObject(sq, isoObject0);
                    } else if (type == IsoObjectType.radio) {
                        isoObject0 = new IsoRadio(cell, sq, spr);
                        AddObject(sq, isoObject0);
                    } else if (spr.getProperties().Is("signal")) {
                        String string0 = spr.getProperties().Val("signal");
                        if ("radio".equals(string0)) {
                            isoObject0 = new IsoRadio(cell, sq, spr);
                        } else if ("tv".equals(string0)) {
                            isoObject0 = new IsoTelevision(cell, sq, spr);
                        }

                        AddObject(sq, isoObject0);
                    } else {
                        if (spr.getProperties().Is(IsoFlagType.WallOverlay)) {
                            IsoObject isoObject2 = null;
                            if (spr.getProperties().Is(IsoFlagType.attachedSE)) {
                                isoObject2 = sq.getWallSE();
                            } else if (spr.getProperties().Is(IsoFlagType.attachedW)) {
                                isoObject2 = sq.getWall(false);
                            } else if (spr.getProperties().Is(IsoFlagType.attachedN)) {
                                isoObject2 = sq.getWall(true);
                            } else {
                                for (int int1 = sq.getObjects().size() - 1; int1 >= 0; int1--) {
                                    IsoObject isoObject3 = sq.getObjects().get(int1);
                                    if (isoObject3.sprite.getProperties().Is(IsoFlagType.cutW) || isoObject3.sprite.getProperties().Is(IsoFlagType.cutN)) {
                                        isoObject2 = isoObject3;
                                        break;
                                    }
                                }
                            }

                            if (isoObject2 != null) {
                                if (isoObject2.AttachedAnimSprite == null) {
                                    isoObject2.AttachedAnimSprite = new ArrayList(4);
                                }

                                isoObject2.AttachedAnimSprite.add(IsoSpriteInstance.get(spr));
                            } else {
                                isoObject0 = IsoObject.getNew();
                                isoObject0.sx = 0.0F;
                                isoObject0.sprite = spr;
                                isoObject0.square = sq;
                                AddObject(sq, isoObject0);
                            }

                            return;
                        }

                        if (spr.getProperties().Is(IsoFlagType.FloorOverlay)) {
                            IsoObject isoObject4 = sq.getFloor();
                            if (isoObject4 != null) {
                                if (isoObject4.AttachedAnimSprite == null) {
                                    isoObject4.AttachedAnimSprite = new ArrayList(4);
                                }

                                isoObject4.AttachedAnimSprite.add(IsoSpriteInstance.get(spr));
                            }
                        } else if (IsoMannequin.isMannequinSprite(spr)) {
                            isoObject0 = new IsoMannequin(cell, sq, spr);
                            AddObject(sq, isoObject0);
                        } else if (type == IsoObjectType.tree) {
                            if (spr.getName() != null && spr.getName().startsWith("vegetation_trees")) {
                                IsoObject isoObject5 = sq.getFloor();
                                if (isoObject5 == null
                                    || isoObject5.getSprite() == null
                                    || isoObject5.getSprite().getName() == null
                                    || !isoObject5.getSprite().getName().startsWith("blends_natural")) {
                                    DebugLog.log("ERROR: removed tree at " + sq.x + "," + sq.y + "," + sq.z + " because floor is not blends_natural");
                                    return;
                                }
                            }

                            isoObject0 = IsoTree.getNew();
                            isoObject0.sprite = spr;
                            isoObject0.square = sq;
                            isoObject0.sx = 0.0F;
                            ((IsoTree)isoObject0).initTree();

                            for (int int2 = 0; int2 < sq.getObjects().size(); int2++) {
                                IsoObject isoObject6 = sq.getObjects().get(int2);
                                if (isoObject6 instanceof IsoTree) {
                                    sq.getObjects().remove(int2);
                                    isoObject6.reset();
                                    isoTreeCache.push((IsoTree)isoObject6);
                                    break;
                                }
                            }

                            AddObject(sq, isoObject0);
                        } else {
                            if ((spr.CurrentAnim.Frames.isEmpty() || ((IsoDirectionFrame)spr.CurrentAnim.Frames.get(0)).getTexture(IsoDirections.N) == null)
                                && !GameServer.bServer) {
                                if (!missingTiles.contains(name)) {
                                    if (Core.bDebug) {
                                        DebugLog.General.error("CellLoader> missing tile " + name);
                                    }

                                    missingTiles.add(name);
                                }

                                spr.LoadFramesNoDirPageSimple(Core.bDebug ? "media/ui/missing-tile-debug.png" : "media/ui/missing-tile.png");
                                if (spr.CurrentAnim.Frames.isEmpty() || ((IsoDirectionFrame)spr.CurrentAnim.Frames.get(0)).getTexture(IsoDirections.N) == null) {
                                    return;
                                }
                            }

                            String string1 = GameServer.bServer
                                ? null
                                : ((IsoDirectionFrame)spr.CurrentAnim.Frames.get(0)).getTexture(IsoDirections.N).getName();
                            boolean boolean1 = true;
                            if (!GameServer.bServer
                                && string1.contains("TileObjectsExt")
                                && (string1.contains("_5") || string1.contains("_6") || string1.contains("_7") || string1.contains("_8"))) {
                                isoObject0 = new IsoWheelieBin(cell, x, y, height);
                                if (string1.contains("_5")) {
                                    isoObject0.dir = IsoDirections.S;
                                }

                                if (string1.contains("_6")) {
                                    isoObject0.dir = IsoDirections.W;
                                }

                                if (string1.contains("_7")) {
                                    isoObject0.dir = IsoDirections.N;
                                }

                                if (string1.contains("_8")) {
                                    isoObject0.dir = IsoDirections.E;
                                }

                                boolean1 = false;
                            }

                            if (boolean1) {
                                isoObject0 = IsoObject.getNew();
                                isoObject0.sx = 0.0F;
                                isoObject0.sprite = spr;
                                isoObject0.square = sq;
                                AddObject(sq, isoObject0);
                                if (isoObject0.sprite.getProperties().Is("lightR")) {
                                    float float3 = Float.parseFloat(isoObject0.sprite.getProperties().Val("lightR"));
                                    float float4 = Float.parseFloat(isoObject0.sprite.getProperties().Val("lightG"));
                                    float float5 = Float.parseFloat(isoObject0.sprite.getProperties().Val("lightB"));
                                    cell.getLamppostPositions()
                                        .add(
                                            new IsoLightSource(
                                                isoObject0.square.getX(), isoObject0.square.getY(), isoObject0.square.getZ(), float3, float4, float5, 8
                                            )
                                        );
                                }
                            }
                        }
                    }
                } else {
                    isoObject0 = new IsoStove(cell, sq, spr);
                    AddObject(sq, isoObject0);
                    GameClient.instance.objectSyncReq.putRequest(sq, isoObject0);
                }
            } else {
                boolean boolean2 = Integer.parseInt(name.substring(name.lastIndexOf("_") + 1)) % 8 <= 3;
                isoObject0 = new IsoCurtain(cell, sq, spr, type == IsoObjectType.curtainN || type == IsoObjectType.curtainS, boolean2);
                AddSpecialObject(sq, isoObject0);
                GameClient.instance.objectSyncReq.putRequest(sq, isoObject0);
            }

            if (isoObject0 != null) {
                isoObject0.tile = name;
                isoObject0.createContainersFromSpriteProperties();
                if (isoObject0.sprite.getProperties().Is(IsoFlagType.vegitation)) {
                    isoObject0.tintr = 0.7F + (float)Rand.Next(30) / 100.0F;
                    isoObject0.tintg = 0.7F + (float)Rand.Next(30) / 100.0F;
                    isoObject0.tintb = 0.7F + (float)Rand.Next(30) / 100.0F;
                }
            }
        }
    }

    public static boolean LoadCellBinaryChunk(IsoCell cell, int wx, int wy, IsoChunk chunk) {
        int int0 = wx;
        int int1 = wy;
        String string = "world_" + wx / 30 + "_" + wy / 30 + ".lotpack";
        if (!IsoLot.InfoFileNames.containsKey(string)) {
            DebugLog.log("LoadCellBinaryChunk: NO SUCH LOT " + string);
            return false;
        } else {
            File file = new File((String)IsoLot.InfoFileNames.get(string));
            if (file.exists()) {
                IsoLot isoLot = null;

                try {
                    isoLot = IsoLot.get(int0 / 30, int1 / 30, wx, wy, chunk);
                    cell.PlaceLot(isoLot, 0, 0, 0, chunk, wx, wy);
                } finally {
                    if (isoLot != null) {
                        IsoLot.put(isoLot);
                    }
                }

                return true;
            } else {
                DebugLog.log("LoadCellBinaryChunk: NO SUCH LOT " + string);
                return false;
            }
        }
    }

    public static IsoCell LoadCellBinaryChunk(IsoSpriteManager spr, int wx, int wy) throws IOException {
        wanderX = 0;
        wanderY = 0;
        wanderRoom = null;
        wanderX = 0;
        wanderY = 0;
        IsoCell isoCell = new IsoCell(300, 300);
        int int0 = IsoPlayer.numPlayers;
        byte _byte = 1;
        if (!GameServer.bServer) {
            if (GameClient.bClient) {
                WorldStreamer.instance.requestLargeAreaZip(wx, wy, IsoChunkMap.ChunkGridWidth / 2 + 2);
                IsoChunk.bDoServerRequests = false;
            }

            for (int int1 = 0; int1 < _byte; int1++) {
                isoCell.ChunkMap[int1].setInitialPos(wx, wy);
                IsoPlayer.assumedPlayer = int1;
                IsoChunkMap isoChunkMap = isoCell.ChunkMap[int1];
                int int2 = wx - IsoChunkMap.ChunkGridWidth / 2;
                isoChunkMap = isoCell.ChunkMap[int1];
                int int3 = wy - IsoChunkMap.ChunkGridWidth / 2;
                isoChunkMap = isoCell.ChunkMap[int1];
                int int4 = wx + IsoChunkMap.ChunkGridWidth / 2 + 1;
                isoChunkMap = isoCell.ChunkMap[int1];
                int int5 = wy + IsoChunkMap.ChunkGridWidth / 2 + 1;

                for (int int6 = int2; int6 < int4; int6++) {
                    for (int int7 = int3; int7 < int5; int7++) {
                        if (IsoWorld.instance.getMetaGrid().isValidChunk(int6, int7)) {
                            isoCell.ChunkMap[int1].LoadChunk(int6, int7, int6 - int2, int7 - int3);
                        }
                    }
                }
            }
        }

        IsoPlayer.assumedPlayer = 0;
        LuaEventManager.triggerEvent("OnPostMapLoad", isoCell, wx, wy);
        ConnectMultitileObjects(isoCell);
        return isoCell;
    }

    private static void RecurseMultitileObjects(
        IsoCell isoCell, IsoGridSquare isoGridSquare1, IsoGridSquare isoGridSquare0, ArrayList<IsoPushableObject> arrayList
    ) {
        Iterator iterator = isoGridSquare0.getMovingObjects().iterator();
        IsoPushableObject isoPushableObject0 = null;
        boolean _boolean = false;

        while (iterator != null && iterator.hasNext()) {
            IsoMovingObject isoMovingObject = (IsoMovingObject)iterator.next();
            if (isoMovingObject instanceof IsoPushableObject) {
                IsoPushableObject isoPushableObject1 = (IsoPushableObject)isoMovingObject;
                int int0 = isoGridSquare1.getX() - isoGridSquare0.getX();
                int int1 = isoGridSquare1.getY() - isoGridSquare0.getY();
                if (int1 != 0 && isoMovingObject.sprite.getProperties().Is("connectY")) {
                    int int2 = Integer.parseInt(isoMovingObject.sprite.getProperties().Val("connectY"));
                    if (int2 == int1) {
                        isoPushableObject1.connectList = arrayList;
                        arrayList.add(isoPushableObject1);
                        isoPushableObject0 = isoPushableObject1;
                        _boolean = false;
                        break;
                    }
                }

                if (int0 != 0 && isoMovingObject.sprite.getProperties().Is("connectX")) {
                    int int3 = Integer.parseInt(isoMovingObject.sprite.getProperties().Val("connectX"));
                    if (int3 == int0) {
                        isoPushableObject1.connectList = arrayList;
                        arrayList.add(isoPushableObject1);
                        isoPushableObject0 = isoPushableObject1;
                        _boolean = true;
                        break;
                    }
                }
            }
        }

        if (isoPushableObject0 != null) {
            if (isoPushableObject0.sprite.getProperties().Is("connectY") && _boolean) {
                int int4 = Integer.parseInt(isoPushableObject0.sprite.getProperties().Val("connectY"));
                IsoGridSquare isoGridSquare2 = isoCell.getGridSquare(
                    isoPushableObject0.getCurrentSquare().getX(),
                    isoPushableObject0.getCurrentSquare().getY() + int4,
                    isoPushableObject0.getCurrentSquare().getZ()
                );
                RecurseMultitileObjects(isoCell, isoPushableObject0.getCurrentSquare(), isoGridSquare2, isoPushableObject0.connectList);
            }

            if (isoPushableObject0.sprite.getProperties().Is("connectX") && !_boolean) {
                int int5 = Integer.parseInt(isoPushableObject0.sprite.getProperties().Val("connectX"));
                IsoGridSquare isoGridSquare3 = isoCell.getGridSquare(
                    isoPushableObject0.getCurrentSquare().getX() + int5,
                    isoPushableObject0.getCurrentSquare().getY(),
                    isoPushableObject0.getCurrentSquare().getZ()
                );
                RecurseMultitileObjects(isoCell, isoPushableObject0.getCurrentSquare(), isoGridSquare3, isoPushableObject0.connectList);
            }
        }
    }

    private static void ConnectMultitileObjects(IsoCell isoCell) {
        Iterator iterator = isoCell.getObjectList().iterator();

        while (iterator != null && iterator.hasNext()) {
            IsoMovingObject isoMovingObject = (IsoMovingObject)iterator.next();
            if (isoMovingObject instanceof IsoPushableObject) {
                IsoPushableObject isoPushableObject = (IsoPushableObject)isoMovingObject;
                if ((isoMovingObject.sprite.getProperties().Is("connectY") || isoMovingObject.sprite.getProperties().Is("connectX"))
                    && isoPushableObject.connectList == null) {
                    isoPushableObject.connectList = new ArrayList();
                    isoPushableObject.connectList.add(isoPushableObject);
                    if (isoMovingObject.sprite.getProperties().Is("connectY")) {
                        int int0 = Integer.parseInt(isoMovingObject.sprite.getProperties().Val("connectY"));
                        IsoGridSquare isoGridSquare0 = isoCell.getGridSquare(
                            isoMovingObject.getCurrentSquare().getX(),
                            isoMovingObject.getCurrentSquare().getY() + int0,
                            isoMovingObject.getCurrentSquare().getZ()
                        );
                        if (isoGridSquare0 == null) {
                            boolean _boolean = false;
                        }

                        RecurseMultitileObjects(isoCell, isoPushableObject.getCurrentSquare(), isoGridSquare0, isoPushableObject.connectList);
                    }

                    if (isoMovingObject.sprite.getProperties().Is("connectX")) {
                        int int1 = Integer.parseInt(isoMovingObject.sprite.getProperties().Val("connectX"));
                        IsoGridSquare isoGridSquare1 = isoCell.getGridSquare(
                            isoMovingObject.getCurrentSquare().getX() + int1,
                            isoMovingObject.getCurrentSquare().getY(),
                            isoMovingObject.getCurrentSquare().getZ()
                        );
                        RecurseMultitileObjects(isoCell, isoPushableObject.getCurrentSquare(), isoGridSquare1, isoPushableObject.connectList);
                    }
                }
            }
        }
    }

    private static void AddObject(IsoGridSquare isoGridSquare, IsoObject isoObject) {
        int _int = isoGridSquare.placeWallAndDoorCheck(isoObject, isoGridSquare.getObjects().size());
        if (_int != isoGridSquare.getObjects().size() && _int >= 0 && _int <= isoGridSquare.getObjects().size()) {
            isoGridSquare.getObjects().add(_int, isoObject);
        } else {
            isoGridSquare.getObjects().add(isoObject);
        }
    }

    private static void AddSpecialObject(IsoGridSquare isoGridSquare, IsoObject isoObject) {
        int _int = isoGridSquare.placeWallAndDoorCheck(isoObject, isoGridSquare.getObjects().size());
        if (_int != isoGridSquare.getObjects().size() && _int >= 0 && _int <= isoGridSquare.getObjects().size()) {
            isoGridSquare.getObjects().add(_int, isoObject);
        } else {
            isoGridSquare.getObjects().add(isoObject);
            isoGridSquare.getSpecialObjects().add(isoObject);
        }
    }
}
